package org.example.chat.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDate;

/**
 * @author cheval
 */
@Component
public class IDGenerate {

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 时间戳 + 序列号
     * @param business
     * @return
     */
    public String nextId(String business) {
        Long beginTimestamp = 1695632171336L;
        Long gutter = System.currentTimeMillis() - beginTimestamp;
        LocalDate.now().toString();
        String IDCounterKey = "ID:" + business + ":" + LocalDate.now().toString();
        Long sequence = redisTemplate.opsForValue().increment(IDCounterKey) ;
        return String.valueOf(gutter << 32 | sequence);
    }
}
